<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.storeid" placeholder="存储服务id" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.storeip" placeholder="ip地址" style="width: 200px;margin-left: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilter">
        Search
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      style="width:100%;margin-top: 15px;"
    >
      <el-table-column align="center" prop="storeId" label="服务ID" min-width="8%" />
      <el-table-column align="center" prop="storeIp" label="Ip" min-width="7%" />
      <el-table-column prop="port" label="端口" min-width="5%" align="center" />
      <el-table-column label="节点名称" min-width="10%" align="center" prop="nodeName" />
      <el-table-column label="状态" min-width="5%" align="center" prop="status" />
      <el-table-column label="心跳时间" min-width="7%" align="center" prop="heartTime" />
      <el-table-column label="操作" min-width="15%" align="center">
        <template #default="scope">
          <el-button size="mini" @click="openBindNodeUi(scope.$index, scope.row)">绑定</el-button>
          <el-button size="mini" @click="unbindNodeUi(scope.$index, scope.row)">解绑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
    <el-dialog v-loading="listLoading" :title="title" :visible.sync="dialogFormVisible" width="30%">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="80px" style="width: 400px; margin-left:50px;">
        <el-form-item label="节点名称" prop="type">
          <el-select v-model="temp.type" class="filter-item" placeholder="请选择节点" @change="selectchange">
            <el-option v-for="item in nodeoptionlist" :key="item.nodename" :label="item.nodename" :value="item.nodename" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="hangdleBindNode">
          绑定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList } from '@/api/store'
import { bindnode } from '@/api/store'
import { removestore } from '@/api/store'
import { unbindstore } from '@/api/store'
import { getnodenamelist } from '@/api/node'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogFormVisible: false,
      total: 0,
      title: '绑定节点',
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        storeid: '',
        storeip: ''
      },
      temp: {
        type: ''
      },
      nodebindAo: {
        nodename: '',
        storeid: ''
      },
      nodeoptionlist: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList({ pageNum: this.listQuery.pageNum, pageSize: this.listQuery.pageSize, storeid: this.listQuery.storeid, storeip: this.listQuery.storeip }).then(response => {
        this.total = response.data.total
        console.log(this.total)
        this.list = response.data.list
        this.listLoading = false
      })
    },
    hangdleBindNode() {
      bindnode({ nodename: this.nodebindAo.nodename, storeid: this.nodebindAo.storeid }).then(response => {
        if (response.code === '0') {
          this.dialogFormVisible = false
          this.showsucess(response.msg)
          this.fetchData()
        }
      })
    },
    showsucess(msg) {
      this.$message({
        message: msg,
        type: 'success',
        duration: 5 * 1000
      })
    },
    getNodeNamelist() {
      getnodenamelist().then(response => {
        console.log('getnodenamelist...........')
        console.log(response)
        this.nodeoptionlist = response.data
      })
    },
    handleFilter() {
      this.fetchData()
    },
    openBindNodeUi(index, row) {
      this.nodebindAo.storeid = row.storeId
      this.dialogFormVisible = true
      this.getNodeNamelist()
    },
    unbindNodeUi(index, row) {
      unbindstore({ storeid: row.storeId }).then(response => {
        if (response.code === '0') {
          this.showsucess(response.msg)
          this.fetchData()
        }
      })
    },
    selectchange(current) {
      console.log('current:', current)
      this.nodebindAo.nodename = current
    },
    handleDelete(index, row) {
      removestore({ storeid: row.storeId }).then(response => {
        this.showsucess(response.msg)
        this.fetchData()
      })
    },
    resetTemp() {
      this.temp = {
        type: ''
      }
    }
  }
}
</script>
